package com.cz.rabbitmq.config;

import cn.hutool.setting.Setting;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author chenzhen
 * @time 2022/11/14
 * @desc
 */

@Component
public class Config {

    @Bean
    public ConnectionFactory connectionFactory(){
        Setting setting = new Setting("config.properties");
        ConnectionFactory factory = new ConnectionFactory();//MQ采用工厂模式来完成连接的创建
        //2.在工厂对象中设置连接信息(ip,port,virtualhost,username,password)
        factory.setHost(setting.get("rabbitmq.host"));//设置MQ安装的服务器ip地址
        factory.setPort(setting.getInt("rabbitmq.port"));//设置端口号
        //MQ通过用户来管理
        factory.setUsername(setting.getStr("rabbitmq.username"));//设置用户名称
        factory.setPassword(setting.getStr("rabbitmq.password"));//设置用户密码
        //3.通过工厂对象获取连接
        return factory;
    }
}
